package applet;

import java.util.ArrayList;
import java.lang.Math;
import processing.opengl.*;
import processing.core.*;

class DynTree
{
	ArrayList<Ball> balls;
	int borderLimit;
	int width,height,x,y;
	public int coreArtistId;
	
	public DynTree(int cx, int cy, int cwidth, int cheight, int cid)
	{
	   //size(500, 500  );
	   x = cx;
	   y = cy;
	   width = cwidth;
	   height = cheight;
	   coreArtistId = cid;
	  GLOBAL.processing.frameRate(60);
	  GLOBAL.processing.smooth();
	  
	  //background(0);
	  borderLimit = 255;
	  balls = new ArrayList<Ball>();
	  GLOBAL.db.getArtistsRecommendations(coreArtistId, GLOBAL.curAgeId, GLOBAL.curSex, GLOBAL.curCountry, 2000, 10);
	  //obtain GLOBAL.artistRecommendationIds
	  balls.add(new Ball(x, y, width, height, 0, balls, GLOBAL.testArtist.id));
	  for(int i=0;i<GLOBAL.artistRecommendationsIds.size();i++)
	  {
		  balls.add(new Ball(x, y, width, height, i+1, balls, GLOBAL.artistRecommendationsIds.get(i)));
	  }
	}
	
	public void updateCoreArtistId(int id)
	{
		coreArtistId = id;
		  GLOBAL.processing.frameRate(60);
		  GLOBAL.processing.smooth();
		  
		  //background(0);
		  borderLimit = 255;
		  balls = new ArrayList<Ball>();
		  //PApplet.println(coreArtistId+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
		  GLOBAL.db.getArtistsRecommendations(coreArtistId, GLOBAL.curAgeId, GLOBAL.curSex, GLOBAL.curCountry, 2000, 10);
		  //obtain GLOBAL.artistRecommendationIds
		  //PApplet.println(GLOBAL.artistRecommendationsIds.get(0)+"!!!!!!!!!!!!!!!!!!!!!!!");
		  for(int i=0;i<=GLOBAL.artistRecommendationsIds.size();i++)
		  { 
			  if(i==0)
				  balls.add(new Ball(x, y, width, height, i, balls, /*GLOBAL.artistRecommendationsIds.get(i)*/id));
			  else
				  balls.add(new Ball(x, y, width, height, i, balls, GLOBAL.artistRecommendationsIds.get(i-1)));
		  }
	}
	 
	public void draw()
	{
	  flush();
	  
		//GLOBAL.processing.stroke(GLOBAL.colorBlu);
		//GLOBAL.processing.fill(0);
		//GLOBAL.processing.rect(x, y, width, height);
	  if (this.mouseOver())
		  {GLOBAL.onDynTree = true;}
	  else
		  GLOBAL.onDynTree = false;
	  
	   for (int i = 0;/*balls.size()-1;*/ i >= 0; i--)
	   {
	    
	      Ball ball = (Ball) balls.get(i);
	      
	      for (int j = balls.size()-1; j >= 0; j--)
	      {
	      // An ArrayList doesn't know what it is storing so we have
	      // to cast the object coming out
	        Ball ball2 = (Ball) balls.get(j);
	        if(PApplet.dist(ball.x,ball.y,ball2.x,ball2.y)<=borderLimit && ball2!=ball)
	        {
	        	GLOBAL.processing.stroke(270-PApplet.norm(PApplet.dist(ball.x,ball.y,ball2.x,ball2.y),0,borderLimit)*255,PApplet.norm(PApplet.dist(ball.x,ball.y,ball2.x,ball2.y),0,borderLimit)*255,0,270-PApplet.norm(PApplet.dist(ball.x,ball.y,ball2.x,ball2.y),0,borderLimit)*255);
	            ball.red = (int)(GLOBAL.processing.max(ball.red,270-PApplet.norm(PApplet.dist(ball.x,ball.y,ball2.x,ball2.y),0,borderLimit)*255));
	            ball.green =(int)(GLOBAL.processing.max(ball.green,PApplet.norm(PApplet.dist(ball.x,ball.y,ball2.x,ball2.y),0,borderLimit)*255));
	          if(ball2.blocked)
	          {
	            ball.blocked = true;
	          }
	          GLOBAL.processing.fill(0);
	          GLOBAL.processing.line(ball.x,ball.y,ball2.x,ball2.y);
	        }
	      }
	    }
	   
	   
	  for (int i = balls.size()-1; i >= 0; i--) {
	      // An ArrayList doesn't know what it is storing so we have
	      // to cast the object coming out
	      Ball ball = (Ball) balls.get(i);
	      ball.draw();
	       
	    }
	    
	}
	
	
	public void flush()
	{
		GLOBAL.processing.pushStyle();
		GLOBAL.processing.noStroke();
		//GLOBAL.processing.noFill();
		//GLOBAL.processing.rect(x,y,width,height);
		GLOBAL.gu.drawBox(x, y, width, height, 0, GLOBAL.processing.color(200), 255);
		GLOBAL.gu.drawBox(x, y, width, height, 1, GLOBAL.processing.color(200), 150);
		GLOBAL.processing.popStyle();
	}
	
	public void redraw()
	{
		for(Ball c: balls)
			c.redraw();
	}

	
	/*public void keyPressed() {
	   
	    if (GLOBAL.processing.keyCode == GLOBAL.processing.keyEvent.VK_DELETE  )
	    {
	      for (int i = balls.size()-1; i >= 0; i--)
	      {
	         Ball ball = (Ball) balls.get(i);
	         ball.blocked=false;
	      }
	    }
	    if (GLOBAL.processing.keyCode == GLOBAL.processing.UP  )
	    {
	     balls.add(new Ball());
	    }
	    if (GLOBAL.processing.keyCode == GLOBAL.processing.DOWN  )
	    {
	     balls.remove(balls.size()-1);
	    }
	     if (GLOBAL.processing.keyCode == GLOBAL.processing.RIGHT  )
	    {
	         borderLimit++;
	    }
	     if (GLOBAL.processing.keyCode == GLOBAL.processing.LEFT  )
	    {
	        borderLimit--;
	    }
	   
	}*/
	
	public boolean mouseOver()
    {
    	if(GLOBAL.processing.mouseX >= x && GLOBAL.processing.mouseX <= x + width && 
    			GLOBAL.processing.mouseY >= y && GLOBAL.processing.mouseY <= y + height) return true;
    	return false;
    }
}